/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Base;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Alina
 */
public class CorreoDB {

    public int grabarCorreo(String correo) throws SQLException {
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        int resultado = stmt.executeUpdate("INSERT INTO CORREO_ELECTRONICO VALUES ('" + correo + "')");
        var.commit();
        db.getConnection().close();
        return resultado;
    }


    public ArrayList<String> buscarCorreos(String cedula) throws SQLException {
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        ResultSet resultado = stmt.executeQuery("SELECT CORREO FROM PERSONA_CORREO WHERE CEDULA='"+cedula+"'");
        ArrayList todos = new ArrayList();
        while (resultado.next())
        {
            todos.add(resultado.getString("CORREO"));
        }
        
        db.getConnection().close();
        return todos;
    }

    public int eliminarCorreoPersona(String cedula) throws SQLException {
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        int resultado = stmt.executeUpdate("DELETE PERSONA_CORREO WHERE CEDULA='" + cedula + "'");
        var.commit();
        db.getConnection().close();
        return resultado;
    }
    
    
        public boolean buscarCorreo(String correo) throws SQLException {
                
        DatabaseManager db = new DatabaseManager();
        Connection var = db.getConnection();
        Statement stmt = var.createStatement();
        String consulta="SELECT * FROM CORREO_ELECTRONICO WHERE CORREO='" + correo + "'";
        ResultSet resultado = stmt.executeQuery(consulta);
        
        if(resultado.next())
        {
            db.getConnection().close();
            return true;
        }

        return false;
    }
}
